<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/23
 * Time: 8:40
 */

namespace app\api\controller\v2;


use app\api\lib\exception\MissException;
use app\api\lib\exception\SuccessMessage;
use app\api\model\Activity as ActivityModel;
use app\api\model\CouponCategory as CouponCategoryModel;
use app\api\validate\IdMustInt;
use app\api\model\Payset;

class ActivityController extends BaseController
{
    public function getActivity()
    {
        $set = Payset::find();
        if(!$set){
            throw new MissException(['msg' => '后台缺少活动相关设置']);
        }else if($set->getData('allow_coupon') != 1){
            throw new MissException(['msg'=>'优惠券活动已关闭']);
        }else{
            $activity = ActivityModel::field('id,head_img,head_img_origin,is_have_coupon,start_time,end_time')->where(['is_home_page'=>1,'is_ongoing'=>1])->order(['create_time'=>'asc'])->find();
            if(!$activity){
                throw new MissException(['msg'=>'没有可应用的活动']);
            }
            $now = date('Y-m-d H:i:s');
            if(($now >= $activity->getData('start_time')) && ($now <= $activity->getData('end_time'))){
                throw new SuccessMessage(['data'=>$activity]);
            }else{
                throw new MissException(['msg'=>'活动不在有效期!']);
            }
        }
    }




    //获取首页的最新活动列表
//    public function getNewActivity()
//    {
//        $activity = ActivityModel::field('id,head_img,is_have_coupon,start_time,end_time')->where(['is_home_page'=>1,'is_ongoing'=>1])->find();
//        if(!$activity){
//            throw new MissException();
//            return;
//        }
//        $now = date('Y-m-d H:i:s');
//        if(($now >= $activity->getData('start_time')) && ($now <= $activity->getData('end_time'))){
//            throw new SuccessMessage(['data'=>$activity]);
//        }else{
//            throw new MissException(['msg'=>'活动不在有效期!']);
//        }
//    }


    //根据活动id返回优惠券的列表
    public function getCoupons($activity_id=null)
    {
        if($activity_id === null){
            (new IdMustInt())->goCheck();
            $id = $this->request->param('id');
        }else{
            $id = $activity_id;
        }
        $couponIds = ActivityModel::field('id,is_have_coupon,coupon_ids,start_time,end_time')->where(['id'=>$id])->find();
        $isHaveCoupon = $couponIds->getData('is_have_coupon');
        switch ($isHaveCoupon){
            case 1:
                $coupons= explode(',',$couponIds->getData('coupon_ids'));
                $coupons = CouponCategoryModel::field('id,name,low_price,amount_cash')->where('id','in',$coupons)->select();
//                foreach($coupons as $k=>$v){
//                    $arr = explode('.',$v->getData('low_price'));
//                    $v->data('low_price',$arr[0]);
//                    $arr = explode('.',$v->getData('amount_cash'));
//                    $v->data('amount_cash',$arr[0]);
//                }
                $data = ['time'=>['activity_id'=>$couponIds->getData('id'),'start_time'=>$couponIds->getData('start_time'),'end_time'=>$couponIds->getData('end_time')],'coupons'=>$coupons];
                throw new SuccessMessage(['data'=>$data]);
                break;
            case 2:
                $coupons= explode(',',$couponIds->getData('coupon_ids'));
                $coupons = CouponCategoryModel::field('id,name,discount_ticket')->where('id','in',$coupons)->select();
                $data = ['time'=>['start_time'=>$couponIds->getData('start_time'),'end_time'=>$couponIds->getData('end_time')],'coupons'=>$coupons];
                throw new SuccessMessage(['data'=>$data]);
                break;
            default:
                throw new MissException(['msg'=>'请求的优惠券信息不存在']);
        }
    }



    //获取新人专享的活动
//    public function getForPeopleActivity()
//    {
//        $activity = ActivityModel::field('id,head_img,is_have_coupon,coupon_ids,start_time,end_time')->where(['activity_type'=>0,'is_ongoing'=>1])->find();
//        if(!$activity){
//            throw new MissException();
//            return;
//        }
//        $now = date('Y-m-d H:i:s');
//        if(($now >= $activity->getData('start_time')) && ($now <= $activity->getData('end_time'))){
//            return $this->getCoupons($activity->getData('id'));
//        }else{
//            throw new MissException(['msg'=>'活动不在有效期!']);
//        }
//    }

    //获取所有活动列表
//    public function getAllActivity()
//    {
//        $result = ActivityModel::where(['is_ongoing'=>1])->field(['id,head_img,start_time,end_time,is_have_coupon'])->order(['end_time'=>'desc'])->limit(8)->select();
//        if($result){
//            throw new SuccessMessage(['data'=>$result]);
//        }else{
//            throw new MissException();
//        }
//    }

    //返回活动的详情(介绍))
//    public function getActivityDetails()
//    {
//        (new IdMustInt())->goCheck();
//        $id = $this->request->param('id');
//
//
//        $res = ActivityModel::field('id,details,is_have_coupon')->where(['id'=>$id])->find();
//
//        if($res){
//            if($res->getData('is_have_coupon') != 0){
//                $this->getCoupons($id);
//            }
//            throw new SuccessMessage(['data'=>$res]);
//        }else{
//            throw new MissException();
//        }
//    }

}